package com.liuhaoqi.raletiveblog.service.Impl;

import com.liuhaoqi.raletiveblog.entity.Test;

import com.liuhaoqi.raletiveblog.mapper.TestMapper;
import com.liuhaoqi.raletiveblog.result.PageResult;
import com.liuhaoqi.raletiveblog.result.WebRestResult;
import com.liuhaoqi.raletiveblog.result.business.PageResult.TestPageResult;
import com.liuhaoqi.raletiveblog.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author liuhaoqi
 * @Date 2020/9/14 18:29
 * @Version 1.0
 */
@Service
public class TestServiceImpl implements TestService {
    @Autowired
    TestMapper testMapper;

    @Override
    public WebRestResult addComment(Test test) {
        WebRestResult result = new WebRestResult();
        test.setCreatetime(new Date());
        test.setUpdatetime(new Date());
        if (testMapper.insertSelective(test) == 1) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("新增评论成功！");
        }
        return result;
    }


    //存放迭代找出的所有子代的集合
    private List<TestPageResult> tempReplys = new ArrayList<>();

    @Override
    public PageResult<TestPageResult> getCommentList(int blogid) {
        List<TestPageResult> testPageResults = testMapper.findByParentIdNull(blogid, -1,0,10000,10000);
        for (TestPageResult testPageResult : testPageResults) {
            int commentid = testPageResult.getCommentid();
            List<TestPageResult> childComments = testMapper.findByParentIdNotNull(commentid,0,10000,10000);
            //查询出子评论
            combineChildren(childComments);
            testPageResult.setReplies(tempReplys);
            tempReplys = new ArrayList<>();
        }
        PageResult<TestPageResult> result = new PageResult<>();
        result.setData(testPageResults);
        result.setTotal(testPageResults.get(0).getCnt());
        return result;
    }

    /**
     * @Description: 查询出子评论
     * @Auther: liuhaoqi
     * @Param: childComments：所有子评论
     * @Return:
     */
    private void combineChildren(List<TestPageResult> childComments) {
        //判断是否有一级子回复
        if(childComments.size() > 0){
            //循环找出子评论的id
            for(TestPageResult childComment : childComments){
                tempReplys.add(childComment);
                int childId = childComment.getCommentid();
                List<TestPageResult> list = testMapper.findByParentIdNotNull(childId, 0, 10000, 10000);
                childComment.setReplies(list);
                //查询二级以及所有子集回复
//                recursively(childId);
            }
        }
        System.out.println();
    }
    /**
     * @Description: 循环迭代找出子集回复
     * @Auther: liuhaoqi
     * @Param: childId：子评论的id
     * @Return:
     */
    private void recursively(int childId) {
        //根据子一级评论的id找到子二级评论
        List<TestPageResult> replayComments = testMapper.findByReplayId(childId,0,10000,10000);
        if(replayComments.size() > 0){
            for(TestPageResult replayComment : replayComments){
                int replayId = replayComment.getCommentid();
                tempReplys.add(replayComment);
                //循环迭代找出子集回复
                recursively(replayId);
            }
        }
    }
}
